package com.woniu.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description:日期工具类
 * @author: my
 * @time: 2021/4/25 14:27
 */
@Slf4j
@Component
public class DateUtil {
    private static final String DATE_PATTERN = "yyyy-MM-dd";

    /**
     * 获取近三十天起始、终止日期
     * @return
     */
    public String[] getStartDay(int month){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,-month);
        Date start = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
        String endDate = sdf.format(date);
        String startDate = sdf.format(start);
        String[] result = {startDate,endDate};
        return result;
    }

    /**
     * 获取指定月数之前的date
     * @param month
     * @return
     */
    public Date getDateByMonth(Integer month){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,-month);
        return calendar.getTime();
    }

    public String getDateString(Date date){
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        return format.format(date);
    }

    public static void main(String[] args) {
        String[] result = new DateUtil().getStartDay(36);
        System.out.println(result[0]+"-"+result[1]);
    }
}
